import request from '@/utils/request'
import store from '@/store'

// 文章详情页 - 获取详情
export const getArticleDetailAPI = ({ id = 1 }) => {
  return request({
    url: '/v1_0/articles/' + id,
    method: 'GET',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    }
  })
}

// 文章详情 - 关注作者
export const followAutAPI = ({ autid }) => {
  return request({
    url: '/v1_0/user/followings',
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    },
    data: {
      target: autid
    }
  })
}

// 文章详情 - 取关作者
export const unfollowAutAPI = ({ autid }) => {
  return request({
    url: `/v1_0/user/followings/${autid}`,
    method: 'DELETE',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    }
  })
}

// 文章详情 - 点赞
export const articleLikeAPI = ({ target }) => {
  return request({
    url: '/v1_0/article/likings',
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    },
    data: {
      target: target
    }
  })
}

// 文章详情 - 取消点赞
export const articleDisLikeAPI = ({ artId }) => {
  return request({
    url: `/v1_0/article/likings/${artId}`,
    method: 'DELETE',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    }
  })
}

// 文章详情 - 获取评论列表
export const getCommentListAPI = ({ artId, offset, limit = 10 }) => {
  return request({
    url: '/v1_0/comments',
    method: 'GET',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    },
    params: {
      type: 'a',
      source: artId,
      offset,
      limit
    }
  })
}

// 文章详情 - 评论点赞
export const commentLikeAPI = ({ comtid }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    },
    data: {
      target: comtid
    }
  })
}

// 文章详情 - 取消评论点赞
export const commentDisLikeAPI = ({ comtid }) => {
  return request({
    url: `/v1_0/comment/likings/${comtid}`,
    method: 'DELETE',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    }
  })
}

// 文章详情 - 发布评论
export const publishCommentAPI = ({ artid, content }) => {
  console.log(artid, content)
  return request({
    url: '/v1_0/comments',
    method: 'POST',
    headers: {
      Authorization: 'Bearer ' + store.state.user.token
    },
    data: {
      target: artid,
      content
    }
  })
}
